//
//  HomeViewController.m
//  智慧小区
//
//  Created by lab603 on 15/6/19.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "HomeViewController.h"
#import "SDCycleScrollView.h"
#import "HomeHeaderView.h"
#import "HomeItemsCell.h"
#import "HomeLikeCell.h"
#import "RepairController.h"
#import "ComplainController.h"
#import "NoticeController.h"
#import "PhoneNumberController.h"
#import "APIClient.h"
#import "PPHttpTool.h"
#import "Advertise.h"
#import "MJExtension.h"
#import "AdvertiseDetailController.h"
#import "Tool.h"
#import "EGOCache.h"
#import "UIImageView+WebCache.h"
#define padding 15
#define Cache_AdvertiseKey @"AdvertiseCache"
#define CacheTime 24*60*60
@interface HomeViewController()<SDCycleScrollViewDelegate,HomeItemsCellDelegate>
//功能选项数组
@property (nonatomic,strong)NSArray *itemsArray;
//猜你喜欢数组
@property (nonatomic,strong)NSArray *likeArray;
//广告的模型数组
@property (nonatomic,strong)NSMutableArray *advertiseArray;
@end
@implementation HomeViewController
- (id)init{
    if (self = [super init]) {
        self.view.backgroundColor = APP_BACK_COLOR;
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
    return self;
}
- (void)viewDidLoad{
    //获得广告列表数据
    [self getAdvertiseData];
    NSArray *itemsArray = @[@"报修",@"投诉建议",@"通知",@"大家庭",@"生活导航",@"常用电话"];
    self.itemsArray = itemsArray;
}
/**
 *  获取广告数据列表
 */
- (void)getAdvertiseData{
    NSString *urlString = [APIClient relativePathForAdvertiseList];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        self.advertiseArray = (NSMutableArray *)[Advertise objectArrayWithKeyValuesArray:json];
        //下载保存图片数据
        [self downloadAndSavePictures];
        [[EGOCache globalCache] setObject:self.advertiseArray forKey:Cache_AdvertiseKey withTimeoutInterval:CacheTime];
        self.tableView.tableHeaderView = [self createHeader];
    } failure:^(NSError *error) {
        
    }];
}
/**
 *  下载保存图片数据
 */
- (void)downloadAndSavePictures{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    for (int i =0; i<self.advertiseArray.count; i++) {
        Advertise *advertise = self.advertiseArray[i];
        NSString *urlString = [NSString stringWithFormat:@"%@%@",StaticFileUrl,advertise.picture];
        [manager downloadImageWithURL:[NSURL URLWithString:urlString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            NSString *keyString = [NSString stringWithFormat:@"imageCache%d",i+1];
            [[EGOCache globalCache] setImage:image forKey:keyString];
        }];
    }
}
/**
 *  设置tableView的Header为轮播器
 */
- (UIView *)createHeader{
    HomeHeaderView *homeHeaderView = [[HomeHeaderView alloc] init];
    NSMutableArray *imagesArray = [NSMutableArray array];
    NSMutableArray *textArray = [NSMutableArray array];
    for (int i =0; i<self.advertiseArray.count; i++) {
        Advertise *advertise = self.advertiseArray[i];
        NSString *imageString = advertise.picture;
        NSString *text = advertise.texts;
        //url拼接
        NSString *imageUrlSting = [NSString stringWithFormat:@"%@%@",StaticFileUrl,imageString];
        //NSURL *imageUrl = [NSURL URLWithString:imageUrlSting];
        [imagesArray addObject:imageUrlSting];
        [textArray addObject:text];
    }
    homeHeaderView.pictureArray = imagesArray;
    homeHeaderView.textArray = textArray;
    homeHeaderView.cycleScrollView.delegate = self;
    homeHeaderView.frame = CGRectMake(0, 0, self.view.bounds.size.width, ScreenWidth*0.55);
    return homeHeaderView;
}
/**
 *  轮播图的代理方法，在点击后触发
 *
 *  @param cycleScrollView 轮播图
 *  @param index           点击了第几张图
 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    IWLog(@"点击了第%ld张图",(long)index);
    Advertise *advertise = self.advertiseArray[index];
    if (advertise.hypelink) {
        AdvertiseDetailController *advertiseVC = [[AdvertiseDetailController alloc] init];
        advertiseVC.advertiseDetail = advertise.hypelink;
        [self.navigationController pushViewController:advertiseVC animated:YES];
    }
}
/**
 *  功能Cell的代理方法
 */
- (void)itemsBtn:(HomeItemsCell *)cell didClick:(UIButton *)Btn{
    switch (Btn.tag) {
        case Repair:{
            RepairController *repairVC = [[RepairController alloc] init];
            [self.navigationController pushViewController:repairVC animated:YES];
        }
            break;
        case Complain:{
            ComplainController *complainVC = [[ComplainController alloc] init];
            [self.navigationController pushViewController:complainVC animated:YES];
        }
            break;
        case Notice:{
            NoticeController *noticeVC = [[NoticeController alloc] init];
            [self.navigationController pushViewController:noticeVC animated:YES];
        }
            break;
        case Family:{
            [Tool HUDShowMessage:@"此功能正在赶工中，尽请期待！"];
        }
            break;
        case LifeGuider:{
            [Tool HUDShowMessage:@"此功能正在赶工中，尽请期待！"];
        }
            break;
        case Phone:{
            PhoneNumberController *phoneVC = [[PhoneNumberController alloc] init];
            [self.navigationController pushViewController:phoneVC animated:YES];
        }
            break;
        default:
            break;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeItemsCell *cell = [HomeItemsCell cellWithTableView:tableView];
    if (indexPath.row==0) {
        cell.itemsArray = self.itemsArray;
        cell.delegate = self;
    }else if(indexPath.row==1){
        /*
        HomeLikeCell *cell = [HomeLikeCell cellWithTableView:tableView];
        cell.dataArray = self.likeArray;
        return cell;
         */
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    if (indexPath.row==0) {
        cellHeight = (ScreenWidth-2*25-2*30)/3*2 +2*15+30 +10;
    }else if(indexPath.row==1){
        /*
        cellHeight = 100 *4+10*3+55;
         */
    }
    return cellHeight;
}
@end
